package SuanFaTest;

import org.junit.Test;

public class QuickSort {

    public static void quickSort(int[] array,int start,int end){
        int p = start;
        int q = end;
        int target = array[p];
        if(p >= q){
            return;
        }
        while (p < q){
            while (p < q && array[q] > target){
                q--;
            }
            while (p < q && array[p] <= target){
                p++;
            }
            if (p < q){//不用具体值来比较，比如数组具体指，为了防止没有比target小的数
                int z = array[q];
                array[q] = array[p];
                array[p] = z;
            }
        }
        //兑换位置后还要循环，直到p=q时跳出循环
        array[start] = array[p];
        array[p] = target;//交换基准值
        quickSort(array,start,q - 1);
        quickSort(array,q + 1,end);//递归调用方法
    }

    @Test
    public void t1(){
        int[] array = new int[]{1,6,8,7,3,5,16,4,8,36,13,44};
        quickSort(array,0,array.length - 1);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }


}
